<?php
namespace Admin\Controller;

use Think\Controller;
use Admin\Model\MemberModel;
use Admin\Model\RegionModel;
use Admin\Model\CityModel;

/**
 * 城市合伙人管理后台
 */
class MemberController extends Controller
{

	//该城市合伙人的id
	private $member_id;

	function __construct(){
		parent::__construct();
        if($_SESSION['member_id']){
			$this->member_id = $_SESSION['member_id'];
			$this->Member = new MemberModel();
		}else{
			returnMobileJson(0,'未登录');
		}

	}

	/**
	 * 获取成员列表
 	 * @param int $offset 分页从第几条开始
	 * @param int $limit 每页条数
	 * @param int $type 成员类型，0-全部，1-城市合伙人，2-联盟商户，3-自有工程师，4-滴滴工程师，5-联盟商户工程师，6-联盟商户门店
	 * @return array $member_list member信息
	 */
	public function get_member_list($offset=0,$limit=10,$type=0){
		$member_list=$this->Member->get_member_list($offset,$limit,$type,$this->member_id);
		if(!$member_list)
			returnMobileJson(201,'没有数据');
		else
			returnMobileJson(200,'获取成功',$member_list);
	}

	/**
	 * 添加联盟商户,工程师
	 * @param  array $data member信息
	 * 必须传入m_type来判断是什么member类型
	 */
	public function add_member(){
		$m_name=$_POST['m_name'];
		$m_account=$_POST['m_account'];
		$m_pwd=$_POST['m_pwd'];
		$m_type=$_POST['m_type'];
		if(empty($m_name) || empty($m_pwd) || empty($m_type)){
			returnMobileJson(201,'参数错误');
		}
		//接收账号并验证是否为手机号码
		if(!preg_match("/^1\d{10}$/",$m_account)){
			returnMobileJson(202,'参数错误,账号需为手机号');
		}

		if($this->Member->get_exist_member($m_account)){
			returnMobileJson(203,'账号已存在');
		}

		// 获取该合伙人的城市
		$member=$this->Member->get_member($this->member_id);


		$data = array( 'm_account'=>$m_account, 'm_pwd'=> md5($m_pwd), 'm_name'=>$m_name,'m_type'=>$m_type, 'm_date' => date('Y-m-d H:i:s') ,'pa_id'=>$this->member_id , 'city_id'=>$member['city_id']);

		if($this->Member->set_member($data)){
			returnMobileJson(200,'添加成功');		
		}else{
			returnMobileJson(204,'添加失败');
		}
	}

	/**
	 * 获取某个联盟,工程师信息
	 * @param array $city 该合伙人的城市信息
	 * @param  array $region 该合伙人的城市开放区域
	 * @return array 详细信息
	 */
	public function get_member(){
		$member_id=$_POST['member_id'];
		if(empty($member_id)){
			returnMobileJson(201,'参数错误');
		}
		$data['basic_message']=$this->Member->get_member($member_id);

		if($data['basic_message']['m_type']!=2){
			// 获取工程师还需要获取城市和地区信息
			$City = new CityModel();
			$city=$City->get_province_city($data['basic_message']['city_id']);
			$data['city']=$city['city'];
			// 获取该合伙人的开放区域region
			$Region = new RegionModel();
			$region=$Region->get_region($data['basic_message']['city_id']);
			$data['region']=$region;
		}else{
			//获取联盟还需获取其门店信息和工程师信息
			$data['union_store']=$this->Member->get_member_list(0,null,6,$member_id);
			$data['union_engineer']=$this->Member->get_member_list(0,null,5,$member_id);
			// 获取每个工程师的所属区域
			foreach ($data['union_engineer'] as $key => $value) {
				// 获取该城市
				$City = new CityModel();
				$city=$City->get_province_city($value['city_id']);
				$data['union_engineer'][$key]['city_name']=$city['city'];
				// 获取该区域
				$Region = new RegionModel();
				$region=$Region->get_region($value['city_id']);
				// 遍历这个开放城市的所以开放区域
				foreach ($region as $re_key => $re_value) {
					// 判断该工程师是在哪个开放区域
					if($value['re_id']==$re_value['re_id'])
						$data['union_engineer'][$key]['region_name']=$re_value['region'];
				}
			}
		}
		returnMobileJson(200,'获取成功',$data);
	}

	/**
	 * 修改某个联盟,工程师信息
	 * @param array $data 详细信息
	 */
	public function edit_member(){
		$member_id=$_POST['member_id'];
		$m_status=$_POST['m_status'];
		$m_account=$_POST['m_account'];
		$m_pwd=$_POST['m_pwd'];
		$m_name=$_POST['m_name'];
		$re_id=$_POST['re_id'];
		$m_settlement_cycle=$_POST['m_settlement_cycle'];
		$m_royalty_ratio=$_POST['m_royalty_ratio'];
		$m_job_num=$_POST['m_job_num'];
		$m_head_imgurl=$_POST['m_head_imgurl'];

//		if(empty($member_id) || !preg_match("/^1\d{10}$/",$m_account) || !preg_match("/^\d+$/",$m_settlement_cycle) || empty($m_royalty_ratio) || empty($m_pwd) ||  empty($m_name) || empty($re_id)){
//			returnMobileJson(201,'参数错误');
//		}
		if($this->Member->get_exist_member($m_account)){
			returnMobileJson(202,'账号已存在');
		}

		($m_status=='0' || $m_status=='1') && $data['m_status'] = $m_status;
		$m_pwd && $data['m_pwd'] = md5($m_pwd);
		$m_name && $data['m_name'] = $m_name;
		$re_id && $data['re_id'] = $re_id;
		$m_settlement_cycle && $data['m_settlement_cycle'] = $m_settlement_cycle;
		$m_royalty_ratio && $data['m_royalty_ratio'] = $m_royalty_ratio;
		$m_job_num && $data['m_job_num'] = $m_job_num;
		$m_head_imgurl && $data['m_head_imgurl'] = $m_head_imgurl;


		if($data['m_royalty_ratio']){
			if(is_numeric($data['m_royalty_ratio'])){
				if($data['m_royalty_ratio']<100 && $data['m_royalty_ratio']>=0){

				}else{
					returnMobileJson(-2,'错误提成比例');
				}
			}else{
				returnMobileJson(-2,'错误提成比例');
			}
		}

//		$data=array( 'm_status'=>$m_status, 'm_account'=>$m_account, 'm_pwd'=>md5($m_pwd), 'm_settlement_cycle'=>$m_settlement_cycle, 'm_royalty_ratio'=>$m_royalty_ratio, 'm_date' => date('Y-m-d H:i:s'), 'm_name'=>$m_name, 're_id'=>$re_id,);

		if($this->Member->set_member($data,$member_id)){
			returnMobileJson(200,'修改成功');		
		}else{
			returnMobileJson(200,'信息未修改');
		}
	}

	/**
	 * 获取该合伙人地区的开放区域
	 * @return array 开放区域信息
	 */
	public function get_region(){
		// 获取该合伙人的城市
		$member=$this->Member->get_member($this->member_id);
		$Region=new RegionModel();
		if($region=$Region->get_region($member['city_id']))
			returnMobileJson(200,'获取成功',$region);
		else
			returnMobileJson(201,'没有数据');
	}

	/**
	 * 添加开放区域
	 */
	public function add_region(){
		$data['region']=$_POST['region'];
		if(empty($data['region']))
			returnMobileJson(201,'参数错误');
		// 获取该合伙人的城市
		$member=$this->Member->get_member($this->member_id);
		$Region=new RegionModel();
		$region_list=$Region->get_region($member['city_id']);
		foreach ($region_list as $key => $region) {
			if($data['region']==$region['region']){
				returnMobileJson(202,'不能重复添加');
			}
		}

		$data['city_id']=$member['city_id'];
		$data['r_date']=date('Y-m-d H:i:s');
		if($Region->set_region($data)){
			returnMobileJson(200,'添加成功');		
		}else{
			returnMobileJson(203,'添加失败');
		}
	}

	/**
	 * 删除开放区域
	 * @param number $re_id 开放区域id
	 */
	public function del_region(){
		$re_id=$_POST['re_id'];
		if(empty($re_id))
			returnMobileJson(201,'参数错误');
		$Region=new RegionModel();
		if($Region->del_region($re_id)){
			returnMobileJson(200,'删除成功');		
		}else{
			returnMobileJson(202,'删除失败');
		}
	}




}